package com.wuqi.dev.ipcdemo;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;

public class MainActivity extends AppCompatActivity {

    private IMyAidlInterface mInterface;

    private ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected (ComponentName name, IBinder service) {
            mInterface = IMyAidlInterface.Stub.asInterface(service);
        }

        @Override
        public void onServiceDisconnected (ComponentName name) {

        }
    };

    private Messenger mMessenger;

    private ServiceConnection msgConn = new ServiceConnection() {
        @Override
        public void onServiceConnected (ComponentName name, IBinder service) {
            mMessenger = new Messenger(service);
        }

        @Override
        public void onServiceDisconnected (ComponentName name) {

        }
    };

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 进程间通信
        // 文件
        // Socket
        // Intent
        Intent service = new Intent(this, RemoteService.class);
        startService(service);

        Intent aidl = new Intent(this, AIDLService.class);
        bindService(aidl, conn, BIND_AUTO_CREATE);

        Intent msgService = new Intent(this, MessengerService.class);
        bindService(msgService, msgConn, BIND_AUTO_CREATE);

    }

    public void socketSend (View view) {
        new Thread(new Runnable() {
            @Override
            public void run () {
                try {
                    Socket socket = new Socket("127.0.0.1", 7890);
                    OutputStream os = socket.getOutputStream();
                    ObjectOutputStream oos = new ObjectOutputStream(os);
                    Person p = new Person();
                    p.setAge(25);
                    p.setName("HELLO");
                    oos.writeObject(p);
                    oos.flush();
                    oos.close();
                    socket.close();

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public void atySend (View view) {
        Intent intent = new Intent(this, SecondActivity.class);
        // 把数据写入到文件中
        try {
            FileOutputStream os = openFileOutput("person", MODE_PRIVATE);
            ObjectOutputStream oos = new ObjectOutputStream(os);
            Person p = new Person();
            p.setAge(25);
            p.setName("HELLO");
            oos.writeObject(p);
            oos.flush();
            oos.close();


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


        startActivity(intent);
    }

    public void aidlClick (View view) {
        try {
            int add = mInterface.add(10, 20);
            Log.d("MainActivity", "add:" + add);

            mInterface.sendUrl("http://www.baidu.com");

            UserBean user = new UserBean();
            user.setId(1);
            user.setAccount("qwert");
            user.setPassword("1qaz");

            mInterface.sendUser(user);


        } catch (RemoteException e) {
            e.printStackTrace();
        }

    }

    public void msgClick (View view) {

        Message msg = Message.obtain(null, 1, 1, 1);
        Bundle data = new Bundle();
        UserBean user = new UserBean();
        user.setId(1);
        user.setAccount("qwert");
        user.setPassword("1qaz");
        data.putParcelable("user", user);
        data.putString("key", "Hello World");
//        msg.obj = user;

        msg.setData(data);
        try {
            mMessenger.send(msg);
        } catch (RemoteException e) {
            e.printStackTrace();
        }


    }
}
